package login;


import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

public class DeleteStudent extends HttpServlet {
    public void wrong1(){
        String msg="学号为空！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    public void wrong2(){
        String msg="该学生不存在！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String number=new String(request.getParameter("number").getBytes("ISO-8859-1"),"UTF-8");
        if(number.equals("")){
            wrong1();
            response.sendRedirect("index.jsp");
        }else{
            try{
            	Connection con = null;
				Statement stmt = null;
				ResultSet rs = null;
				Class.forName("com.mysql.cj.jdbc.Driver"); //// 驱动程序名
				String url = "jdbc:mysql://localhost:3306/htuser?serverTimezone=UTC "; // 数据库名
				String username = "root"; // 数据库用户名
				String password1 = "123456"; // 数据库用户密码
				con = DriverManager.getConnection(url, username, password1);
//    	                String url = "jdbc:sqlserver://localhost;databaseName=person";
//    	            	String userString="sa";
//    	                String password1="521212";
//    	                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//    		        	con=DriverManager.getConnection(url,userString,password1);
				stmt = con.createStatement();
				String sql = "select * from student where number='"+number+"'";
				rs=stmt.executeQuery(sql);
                rs.last();
                int k;
                k=rs.getRow();
                if(k==0){
                    wrong2();
                    response.sendRedirect("index.jsp");
                }else{
                	String sql2="delete from student where number='"+number+"'";//sql  语句
                    stmt.executeUpdate(sql2);
                }
                rs.close();
                stmt.close();
                con.close();
                response.sendRedirect("index.jsp");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doGet(request, response);
    }

   
}
